package com.zxmr.hospital.info.action;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.component.Page;
import com.zxmr.domain.component.PageForUser;
import com.zxmr.domain.service.ComponentService;
import com.zxmr.domain.service.UserService;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.UserTraceManager;
import com.zxmr.util.Utils;

public class PageAction extends Executor{

	enum method{
		add,
		delete,
		query,
		avaiablePage;
	}

	@Override
	public void execute(Request request, Response response) {
		
		try
		{
			method method = Enum.valueOf(method.class, this.getElement().getMethod());
			switch(method)
			{
			case add:
				add(request,response);
				break;
			case query:
				query(request,response);
				break;
			case delete:
				delete(request,response);
			case avaiablePage:
				queryAvaiable(request,response);
			}

		}catch(Exception e)
		{
			ResponsePayload payload = new ResponsePayload();
			if(e instanceof ExecuteException)
			{
				payload.setValue(e.getMessage());
			}else
			{
				e.printStackTrace();
				payload.setValue("internal error");
			}
			response.setPayload(payload);
			response.setResult(ResultCode.COMPONENT_ERROR);
		}
	}
	
	private void add(Request request,Response response)throws ExecuteException
	{
		PageForUser page = parseRequestToPage(request);
		validateAdd(page,request);
		
		page.setUser(request.getAccount().getId());
		UserService service = new UserService();
		
		if(!service.addPageForUser(page))
			this.internalErrorResponse(response);
		else
		{
			String action = ResourceManager.getInstance().getMessage("com.zxmr.info.addPage",page.getName());
			UserTraceManager.addTrace(action, request.getAccount());
		}
		
		query(request,response);
	}
	private void validateAdd(PageForUser page,Request request)throws ExecuteException
	{
		if(page == null)throw new ExecuteException("internal error");
		
		ComponentService service = new ComponentService();
		Page p = service.findPage(page.getPage());
		if(p == null)throw new ExecuteException("page is not exist");
		
		if(!service.isCompanyAuthorized(request.getAccount().getCompany(), p.getComponent()))
			throw new ExecuteException("you are not authorized to add the page");
		
		if(Utils.isStringEmptyWithTrim(page.getName()))
			page.setName(p.getDisplay());
	}
	private void delete(Request request,Response response)throws ExecuteException
	{
		int id = 0;
		try
		{
			id = Integer.parseInt(request.getRequestParameters().get("id"));
		}catch(Exception e)
		{
			throw new ExecuteException("invalid parameter");
		}
		
		UserService service = new UserService();
		PageForUser pfu = new PageForUser();
		pfu.setId(id);
		pfu.setUser(request.getAccount().getId());
		service.deletePageForUser(pfu);
		
		String action = ResourceManager.getInstance().getMessage("com.zxmr.info.deletePage",pfu.getName());
		UserTraceManager.addTrace(action, request.getAccount());
	}
	private void query(Request request,Response response)throws ExecuteException
	{
		UserService service = new UserService();
		List<PageForUser> pages = service.getAllPageForUser(request.getAccount().getId());
		ResponsePayload payload  = new ResponsePayload(JSON.toJSONString(pages));
		response.setPayload(payload);
	}
	private PageForUser parseRequestToPage(Request request)throws ExecuteException
	{
		try
		{
			return JSON.parseObject(request.getRequestParameters().get("parameters"), PageForUser.class);
		}catch(Exception e)
		{
			throw new ExecuteException("parameter is not valid");
		}
	}
	
	private void queryAvaiable(Request request,Response response)throws ExecuteException
	{
		ComponentService service = new ComponentService();
		List<Page> pages = service.findAllPageForCompany(request.getAccount().getCompany());
		ResponsePayload payload  = new ResponsePayload(JSON.toJSONString(pages));
		response.setPayload(payload);
	}
}
